<?php

namespace App\Libraries\Controller;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Validator;

class BaseController extends Controller
{
    protected $services;

    //region 返回方法

    /**
     * 返回成功消息.
     *
     * @param string            $message 消息
     * @param object|array|null $data    数据
     *
     * @return array
     */
    public function Success($message, $data = null)
    {
        $result = array('flag' => true, 'status' => 'success', 'message' => $message);
        if (null !== $data) {
            $result['data'] = $data;
        }

        return $result;
    }

    /**
     * 返回失败消息.
     *
     * @param string $message   消息
     * @param string $exception 异常
     *
     * @return array
     */
    public function Error($message, $exception)
    {
        return array('flag' => false, 'status' => 'failed', 'message' => $message, 'exception' => $exception);
    }

    //endregion方法方法

    //region 获取数据

    /**
     * 获取分页列表.
     *
     * @return mixed
     */
    public function getPageList()
    {
        return $this->services->GetPageList();
    }

    //endregion

    //region 提交数据

    /**
     * 修改状态
     *
     * @param $id
     *
     * @return mixed
     */
    public function changeStatus($id)
    {
        return $this->services->ChangeStatus($id);
    }

    /**
     * 创建.
     *
     * @param Request $request
     *
     * @return array
     */
    public function create(Request $request)
    {
        $requestData = $request->all();
        //获取验证规则
        $rules = $this->getCreateRules();
        //验证
        $validate = $this->validate($requestData, $rules);
        if ($validate['flag']) {
            return $this->services->Create($requestData);
        } else {
            return $validate;
        }
    }

    /**
     * 更新.
     *
     * @param $id
     * @param Request $request
     *
     * @return array
     */
    public function update($id, Request $request)
    {
        $requestData = $request->all();
        $requestData['id'] = $id;
        //获取验证规则
        $rules = $this->getUpdateRules();
        //验证
        $validate = $this->validate($requestData, $rules);
        if ($validate['flag']) {
            return $this->services->Update($id, $requestData);
        } else {
            return $validate;
        }
    }

    //endregion

    //region 验证数据

    /**
     * 请求字段验证
     *
     * @param $data
     * @param $rules
     *
     * @return array
     */
    public function validate($data, $rules)
    {
        $messages = $this->getRuleMessages();
        $validator = Validator::make($data, $rules, $messages);
        if ($validator->fails()) {
            $errors = $validator->errors();

            return $this->Error($errors->first(), $validator->errors());
        } else {
            return $this->Success('');
        }
    }

    //endregion

    /**
     * 错误信息.
     *
     * @return array
     */
    public function getRuleMessages()
    {
        return [];
    }

    /**
     * 封装创建时验证规则.
     *
     * @return array
     */
    public function getCreateRules()
    {
        return [];
    }

    /**
     * 封装更新时验证规则.
     *
     * @return array
     */
    public function getUpdateRules()
    {
        return [];
    }
}
